package GradeTopN;

import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

public class GradeTopNFlowBean implements WritableComparable<GradeTopNFlowBean> {
    private String grade;
    private double annualIncome;

    //2  反序列化时，需要反射调用空参构造函数
    public GradeTopNFlowBean() {
    }

    //3  序列化方法
    @Override
    public void write(DataOutput dataOutput) throws IOException {
        dataOutput.writeDouble(annualIncome );
        dataOutput.writeUTF(grade);
    }

    //4 反序列化方法
    //5 反序列化方法读顺序必须和写序列化方法的写顺序必须一致
    @Override
    public void readFields(DataInput dataInput) throws IOException {
        annualIncome  = dataInput.readDouble();
        grade = dataInput.readUTF();
    }

    // 6 编写toString方法，方便后续打印到文本
    @Override
    public String toString() {
        return annualIncome +"\t" + grade;
    }

    public GradeTopNFlowBean(double annualIncome, String grade) {
        this.annualIncome  = annualIncome;
        this.grade = grade;
    }

    public double getAnnualIncome() {
        return annualIncome;
    }

    public void setAnnualIncome(int annualIncome) {
        this.annualIncome = annualIncome;
    }

    public String getGrade() {
        return grade;
    }

    public void setGrade(String grade) {
        this.grade = grade;
    }

    @Override
    public int compareTo(GradeTopNFlowBean o) {
        int result;

        result = Double.compare(o.getAnnualIncome(), this.annualIncome);

        return result;
    }
}
